package net.jforum.util.legacy.clickstream.config;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Loads clickstream.xml for JForum.
 * 
 * @author <a href="plightbo@hotmail.com">Patrick Lightbody</a>
 * @author Rafael Steil (little hacks for JForum)
 * @version $Id$
 */
public class ConfigLoader {
	private ClickstreamConfig config;

	private static ConfigLoader instance = new ConfigLoader();;

	public static ConfigLoader instance() {
		return instance;
	}

	private ConfigLoader() {
	}

	public ClickstreamConfig getConfig() {
		if (this.config != null) {
			return this.config;
		}

		synchronized (instance) {
			this.config = new ClickstreamConfig();

			try {
				SAXParser parser = SAXParserFactory.newInstance()
						.newSAXParser();

				String path = SystemGlobals
						.getValue(ConfigKeys.CLICKSTREAM_CONFIG);

				if (path != null) {

					File fileInput = new File(path);

					if (fileInput.exists()) {
						parser.parse(fileInput, new ConfigHandler());
					} else {
						parser
								.parse(new InputSource(path),
										new ConfigHandler());
					}
				}
				return config;
			} catch (SAXException e) {
				throw new RuntimeException(e.getMessage());
			} catch (IOException e) {
				throw new RuntimeException(e.getMessage());
			} catch (ParserConfigurationException e) {
				throw new RuntimeException(e.getMessage());
			} catch (RuntimeException e) {
				throw e;
			} catch (Throwable e) {
				throw new RuntimeException(e.getMessage());
			}
		}
	}

	/**
	 * SAX Handler implementation for handling tags in config file and building
	 * config objects.
	 */
	private class ConfigHandler extends DefaultHandler {
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (qName.equals("bot-host")) {
				config.addBotHost(attributes.getValue("name"));
			} else if (qName.equals("bot-agent")) {
				config.addBotAgent(attributes.getValue("name"));
			}
		}
	}
}
